package bibbletex.helpers;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;

public class BitlyHelper {
	
	public static String shorten(String id){
		URL url;
		String line = null;
		String longurl = "http://134.106.56.197:8081/collaboration/show.jsp?cid="+id;
		try {
			String longurlencoded = URLEncoder.encode(longurl, "UTF-8");

			url = new URL("http://api.bit.ly/v3/shorten?login=xcerptit2012&apiKey=R_d37ec05851a40a099cc099dd679b62e5&longUrl="+longurlencoded+"&format=txt");
			URLConnection connection;
				connection = url.openConnection();
			
			BufferedReader in;
			in = new BufferedReader(
					new InputStreamReader(connection.getInputStream()));
			
			do{
				line = in.readLine();
			}while (line == null);
		
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return line;
	}

}
